﻿using MatTrace;
using MatTrace.Inputs;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace Zt.QmtqDemo.ConfmPlan.Saga
{
    /// <summary>
    /// 物料跟踪
    /// </summary>
    public class MatTraceTask : StepBody, ITransientDependency
    {
        private ITmrbr001AppService tmrbr001;

        public MatTraceTask(ITmrbr001AppService tmrbr001AppService)
        {
            tmrbr001 = tmrbr001AppService;
        }

        public string PlanNo { get; set; }

        public List<ConfmMatDto> MatDtos { get; set; }

        public string Message { get; set; }

        public int Code { get; set; }

        public override ExecutionResult Run(IStepExecutionContext context)
        {
            List<Trace001Input> trace001Inputs = new List<Trace001Input>();
            foreach(var item in MatDtos)
            {
                trace001Inputs.Add(new Trace001Input() { MatNo = item.MatNo, PlanNo = this.PlanNo });
            }
            try
            {
                var matTrace = tmrbr001.Trace(trace001Inputs).Result;
                if (matTrace.Status)
                {
                    return ExecutionResult.Next();
                }
                else
                {
                    this.Message = matTrace.Message;
                    this.Code = -1;
                    throw new BusinessException("500", this.Message);
                }
            }
            catch(Exception ex)
            {
                throw new BusinessException("500", this.Message);

            }
        }
    }
}
